package com.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usercenter.common.BaseResponse;
import com.usercenter.common.ErrorCode;
import com.usercenter.common.ResultUtils;
import com.usercenter.contant.UserConstant;
import com.usercenter.exception.BusinessException;
import com.usercenter.model.User;
import com.usercenter.model.request.UserLoginRequest;
import com.usercenter.model.request.UserRegisterRequest;
import com.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static javafx.scene.input.KeyCode.L;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Resource
    private UserService userService;
    
    
    
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterResult){
        if(userRegisterResult == null){
            throw  new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userRegisterResult.getUserAccount();
        String userPassword = userRegisterResult.getUserPassword();
        String checkPassword = userRegisterResult.getCheckPassword();
        String planetCode = userRegisterResult.getPlanetCode();
        if(StringUtils.isAllEmpty(userAccount,userPassword,checkPassword,planetCode)){
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        }
        Long result=  userService.UserRegister(userAccount, userPassword, checkPassword,planetCode);
       // return new BaseResponse<>(200,result,"OK");
        return result == -1 ? ResultUtils.error(ErrorCode.PANARENT_ERROR) : ResultUtils.success(result);
    }
    
    
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if(userLoginRequest == null){
            throw  new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if(StringUtils.isAllEmpty(userAccount,userPassword)){
            return new BaseResponse<>(-1,null,"Account or Password is null");
        }
        User result = userService.UserLogin(userAccount,userPassword,request);
        return result == null ? ResultUtils.error(ErrorCode.PANARENT_ERROR) : ResultUtils.success(result);
    }
    
    @GetMapping("/search")
    public BaseResponse<List<User>> searchUser(String username,HttpServletRequest request){
        if(!isAdmin(request)){
            throw  new BusinessException(ErrorCode.PANARENT_ERROR,"不是管理员");
        }
        
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(username)){
            queryWrapper.like("username",username);
        }
        List<User> userList = userService.list(queryWrapper);
        List<User> result =  userList.stream().map(user -> userService.getSafeUser(user)).collect(Collectors.toList());
        return ResultUtils.success(result);
    }
    
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody long id,HttpServletRequest request){
        if(!isAdmin(request)){
            throw  new BusinessException(ErrorCode.PANARENT_ERROR,"不是管理员");
        }
        
        if(id <= 0){
            throw  new BusinessException(ErrorCode.PANARENT_ERROR,"没有这个账户");
        }
        Boolean result =  userService.removeById(id);
        return ResultUtils.success(result);
    }
    private boolean isAdmin(HttpServletRequest request){
        //仅管理员可以取
        User user = (User) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
//        if(user == null &&  != UserConstant.ADMIN_ROLE){
//            return false;
//        }
        if(user != null && user.getUserRole() != UserConstant.ADMIN_ROLE){
            return false;
        }
        return true;
    }
    
    
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        if(request == null){
            return null;
        }
        int result =  userService.UserLogout(request);
        return ResultUtils.success(result);
    }
}
